package com.hex.tcpproxy.hx;

import com.hex.tcpproxy.process.MainServer;
import com.hex.tcpproxy.util.ProxyHandler;
import com.hex.tcpproxy.util.ProxyServer;

/**
 * @author: [wujie]
 * @createTime: [2019/10/10 16:28]
 * @description:
 */
public class SalveServer implements Runnable {
    private Integer port;
    private String address;
    private MasterServer masterServer;
    private ProxyHandler proxyHandler;
    private ProxyServer server;

    public SalveServer(int port, String address, MasterServer masterServer) {
        this.port = port;
        this.address = address;
        this.masterServer = masterServer;
    }


    @Override
    public void run() {
        proxyHandler = new ProxyHandler() {
            @Override
            public void read(String address, byte[] bytes) {
                masterServer.write(address, SalveServer.this.address, bytes);
            }
        };
        server = new ProxyServer(port, proxyHandler);
        server.bind();
    }

    public void write(String address, byte[] data) {
        proxyHandler.write(address, data);
    }

    public String getRemoteAdress() {
        return address;
    }

    public void close() {
        server.close();
    }
}
